package cn.bonoon.controllers.fmm;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.core.plugins.AccountService;
import cn.bonoon.core.plugins.RoleService;
import cn.bonoon.entities.AccountEntity;
import cn.bonoon.kernel.menus.ChannelDefinition;
import cn.bonoon.kernel.menus.MenuDefinition;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.DialogModel;
import cn.bonoon.kernel.web.models.JsonResult;

@Controller
@RequestMapping("s/sys/admin")
public class AdminPrivilegeController extends AbstractController {
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ModuleManager moduleManager;
	
	@RequestMapping(value = "!{mid}/adminPrivilege.do", method = { POST, GET })
	public ModelAndView get(
			HttpServletRequest request,
			@PathVariable("mid") String mid, 
			@RequestParam("id") Long id, 
			@RequestParam("gridid") String gridid) {
		
		DialogModel model = new DialogModel(mid, request);
		model.addForm(id);
		model.addObject("gridid", gridid);
		model.addObject("id", id);
		
		AccountEntity account = accountService.get(id);
		
		model.addObject("name", account.getName());
		model.addObject("loginName", account.getLoginName());
		
		ChannelDefinition channelInfo = moduleManager.getChannelManager();
		List<String> _menus = accountService.getMenuIds(id, channelInfo);
		
		StringBuffer html = new StringBuffer();
		for (MenuDefinition menu : channelInfo) {
			String menuid = menu.getId();
			html.append("<input name='admin_menu' value='").append(menuid).append("' ");
			if (_menus.contains(menuid)) {
				html.append(" checked='true' ");
			}
			html.append(" type='checkbox' />&nbsp;").append(menu.getName()).append("<br/>");
		}
		
		model.addObject("privileges", html);
		model.addObject("channelId", channelInfo.getId());
		model.addObject("moduleId", moduleManager.getId());
		
		return model.execute("sys/account/admin-privilege");
	}

	@ResponseBody
	@RequestMapping(value = "!{mid}/adminPrivilege!save.b", method = POST)
	public final JsonResult assign(
			@PathVariable("mid") String mid, 
			@RequestParam("moduleId") String moduleId, 
			@RequestParam("channelId") String channelId, 
			@RequestParam("id") final Long id, 
			@RequestParam("privileges") String privileges) {
		try{
			roleService.saveAdminPrivilege(getUser(), id, moduleId, channelId, privileges.split(","));
			return JsonResult.result();
		}catch(Exception ex){
			return JsonResult.error(ex);
		}
	}
	
}
